﻿using System.Collections;
using UnityEngine;
using TMPro;
using DG.Tweening;

public class PopupText : MonoBehaviour
{
    [SerializeField]
    private TextMeshProUGUI textMeshPro;
    [SerializeField]
    private float riseHeight = 100;
    [SerializeField]
    private float scaleUpDuration = 0.3f;
    [SerializeField]
    private Vector3 scaleUp = new Vector3(1, 1, 1);
    [SerializeField]
    private float scaleDownDuration = 0.2f;
    [SerializeField]
    private Vector3 scaleDown = new Vector3(0.3f, 0.3f, 0.3f);
    // Use this for initialization
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {

    }
    public void Popup(string text, Color? color = null)
    {
        textMeshPro.text = text;
        if (color != null)
        {
            textMeshPro.color = (Color)color;
        }
        Sequence sequence = DOTween.Sequence();
        sequence.Append(transform.DOScale(scaleUp, scaleUpDuration).SetEase(Ease.InQuad));
        sequence.Append(transform.DOScale(scaleDown, scaleDownDuration).SetEase(Ease.OutQuad));
        Tweener tweener = transform.DOMoveY(transform.position.y + riseHeight, scaleUpDuration + scaleDownDuration);
        textMeshPro.DOColor(new Color(textMeshPro.color.a, textMeshPro.color.g, textMeshPro.color.b, 0), scaleUpDuration + scaleDownDuration).SetEase(Ease.InQuad);
        Invoke("DestroySelfByOverTime", scaleUpDuration + scaleDownDuration + 0.1f);
    }
    private void DestroySelfByOverTime()
    {
        Destroy(gameObject);
    }
}
